﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SkyBase
{
    class Bullet : GameObject
    {

        public int damage { get; set; }

        public bool removeMe = false;
        GameObject source;

        public Bullet(Vector2 locationIn, Texture2D spriteIn, World worldIn, int damageIn, GameObject sourceIn) : base("Bullet", locationIn, spriteIn, false, worldIn)
        {
            Debugger.enabled = true;
            
            damage = damageIn;
            checkForCollision = true;
            ghost = true;
            priority = 0;
            source = sourceIn;

        }

        public override void onCollision(GameObject collidee)
        {
            if(!(collidee is Player) && !(collidee.Name.Contains("cliff")))
                removeMe = true;
            if ((collidee is Enemy) && (source is Player))
            {
                if (source is Enemy) { }
                else
                {
                    Enemy e = (Enemy)collidee;
                    e.Health -= damage;
                    //if (damage != 0)
                    //    Debugger.writeLine("Dealing " + damage + " damage to " + e.Name+": "+e.Health);
                    damage = 0;
                    if (e.Health <= 0)
                        world.toRemove.Add(e.Name);
                    removeMe = true;
                }
            }
            else if ((collidee is Player) && (source is Enemy))
            {
                if (!(collidee is Enemy))
                {
                    World.player.Health -= damage;
                    damage = 0;
                    if (World.player.Health <= 0)
                    {
                        World.isPlayerAlive = false;
                    }
                    removeMe = true;
                }
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Sprite, new Vector2((int)Location.X, (int)Location.Y),
                            null, color, (float)GetRotation(),
                            new Vector2(Sprite.Width / 2, Sprite.Height / 2), 0.5f, SpriteEffects.None, priority);
        }
        
    }
}
